﻿Imports System.Text.RegularExpressions

Public Class AmericaTV
    Inherits WebSite

    Public Sub New()
        MyBase.BaseURL = "http://www.americatv.com.pe/portal/multimedia"
        MyBase.Name = "América TV (Perú)"
        MyBase.Site = WebSites.AmericaTV
    End Sub

    Public Overrides Function GetCategories() As System.Collections.Generic.List(Of Category)
        Dim cats As New List(Of Category)
        Dim webData = SiteUtilBase.GetWebData(MyBase.BaseURL)
        Dim m = Regex.Match(webData, "<li><a\shref=""javascript:;""\sclass=""normal-tab[^""]*""\srel=""[^""]*""\sname=""(?<url>[^""]*)""><span>(?<title>[^<]*)</span></a></li>")
        While m.Success
            Dim newCat = New RssLink()
            newCat.Name = m.Groups("title").Value
            newCat.Url = m.Groups("url").Value
            newCat.HasSubCategories = False
            newCat.Other = Sections.InsideWebSite

            cats.Add(newCat)

            m = m.NextMatch()
        End While

        Return cats
    End Function

    Public Overrides Function GetSubCategories(parentCategory As Category) As System.Collections.Generic.List(Of Category)
        Return New List(Of Category)
    End Function

    Public Overrides Function GetVideos(category As Category) As List(Of VideoInfo)
        Dim videos As New List(Of VideoInfo)

        ' TODO: Live videos do not work yet.
        Dim webData = SiteUtilBase.GetWebData("http://www.americatv.com.pe/portal/videostaxonomia/" + CType(category, RssLink).Url)
        Dim m = Regex.Match(webData, "<img\ssrc=""(?<imageUrl>[^""]*)""\s*/></a><p><a\shref=""(?<videoUrl>[^""]*)"">(?<title>[^<]*)</a>")
        While m.Success
            Dim video = New VideoInfo()
            video.Title = m.Groups("title").Value
            video.VideoUrl = m.Groups("videoUrl").Value
            video.ImageUrl = m.Groups("imageUrl").Value

            videos.Add(video)

            m = m.NextMatch()
        End While

        Return videos
    End Function

    Public Overrides Function GetVideoURL(video As VideoInfo) As String
        Dim webData = SiteUtilBase.GetWebData(TVALaCartaUtil.CombinePath("http://www.americatv.com.pe", video.VideoUrl))
        Dim m = Regex.Match(webData, "file=(?<url>[^&]*)&")

        If m.Success Then
            Return m.Groups("url").Value
        Else
            Return ""
        End If
    End Function
End Class
